package com.healthcloud.service.impl;

import java.util.ArrayList;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.healthcloud.dao.DisabilityMapper;
import com.healthcloud.entity.Disability;
import com.healthcloud.service.DisabilityService;

/**
 * Created by XYC on 2015/7/17.
 */
@Service
public class DisabilityServiceImpl implements DisabilityService {

    @Autowired
    private DisabilityMapper disabilityMapper;

    @Override
    public ArrayList<Disability> findByIdCard(Disability disability) {
        Disability disability1 = new Disability();
        disability1.setIdCard(disability.getIdCard());
        return findByEntity(disability1);
    }

    @Override
    public ArrayList<Disability> findByEntity(Disability disability) {
        return disabilityMapper.findByEntityWithOutPage(disability);
    }

    @Override
    public Disability saveOrUpdata(Disability disability) {
        ArrayList<Disability> disabilities = findByIdCard(disability);
        if (disabilities.isEmpty()){
            disability.setUpdatedTime(new Date());
            disability.setCreatedTime(new Date());
            disabilityMapper.save(disability);
        }else{
            disability.setId(disabilities.get(0).getId());
            disability.setCreatedTime(disabilities.get(0).getCreatedTime());
            disability.setUpdatedTime(new Date());
            disabilityMapper.update(disability);
        }
        return disability;
    }
}
